#-*-coding:utf-8-*-

import wx
import sys, os, string, time
import RecorderCtrl, Timer

class RecorderFrame(wx.Frame):
    def __init__(self,parent,title):

        self.threads = []
        self.isPaused = 0

        wx.Frame.__init__(self, parent, -1, title=title, size=(400,400),
                          style=wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN)
        self.panel = wx.Panel(self, -1)

        self.staticbox = wx.StaticBox(self.panel, -1, u"recorder",pos=(0,0),size=(400,300))
        self.serverLabel = wx.StaticText(self.panel, -1, u"mux server:",pos=(20,20))
        self.serverText = wx.TextCtrl(self.panel, -1, "",pos=(100,20),size=(140,20))
        self.portLabel = wx.StaticText(self.panel, -1, u"port:",pos=(260,20))
        self.portText = wx.TextCtrl(self.panel, -1, "",pos=(300,20),size=(80,20))
        self.channelLabel = wx.StaticText(self.panel, -1, u"channel ID:",pos=(20,45))
        self.channelText = wx.TextCtrl(self.panel, -1, u"",pos=(100,45),size=(280,20))
        self.dirButton = wx.Button(self.panel,-1,u"Browse",pos=(320,70),size=(60,20))
        self.pathLabel = wx.StaticText(self.panel, -1, u"saving path:",pos=(20,70))
        self.pathText = wx.TextCtrl(self.panel, -1, os.getcwd(),pos=(100,70),size=(210,20))
        self.infoLabel = wx.StaticText(self.panel, -1, u"recording information",pos=(155,145))
        self.info = wx.TextCtrl(self.panel, -1, "",pos=(0,165),size=(395,110), style=wx.TE_MULTILINE|wx.TE_READONLY)
        self.recordButton=wx.Button(self.panel,-1,u"Record",pos=(60,310),size=(60,30))
        self.pauseButton=wx.Button(self.panel,-1,u"Pause",pos=(170,310),size=(60,30))
        self.exitButton=wx.Button(self.panel,-1,u"Exit",pos=(280,310),size=(60,30))

        self.timerLabel = wx.StaticText(self.panel, -1, u"Time:",pos=(10,280))
        self.timeLabel = wx.StaticText(self.panel, -1, u"0:0:0",pos=(45,280))
        self.videoLabel = wx.StaticText(self.panel, -1, u"video.avi Size:",pos=(90,280))
        self.vSizeLabel = wx.StaticText(self.panel, -1, u"0",pos=(170,280))
        self.screenLabel = wx.StaticText(self.panel, -1, u"screen.avi Size:",pos=(240,280))
        self.sSizeLabel = wx.StaticText(self.panel, -1, u"0",pos=(325,280))
        self.autoLabel = wx.StaticText(self.panel, -1, u"automatically exit while recording completed",pos=(20,120))
        self.autoCheck = wx.CheckBox(self.panel, -1, pos=(290,120))
        self.choices = ["video.avi: video+audio; screen.avi:screen+audio",
                        "video.avi: video; screen.avi:screen+audio",
                        "video.avi: video+audio; screen.avi:screen"]
        self.typeLabel = wx.StaticText(self.panel, -1, u"AVI type",pos=(20,95))
        self.typeChoice = wx.Choice(self.panel, -1, pos = (100, 95), size = (280, 20), choices = self.choices)

        self.vSize = 0
        self.sSize = 0

        self.Bind(wx.EVT_BUTTON, self.Browse, self.dirButton)
        self.Bind(wx.EVT_BUTTON, self.Validate, self.recordButton)
        self.Bind(wx.EVT_BUTTON, self.Exit, self.exitButton)
        self.Bind(wx.EVT_BUTTON, self.Pause, self.pauseButton)

    def GetState(self):
        return self.isPaused;

    def Pause(self, event):
        self.isPaused = 1-self.isPaused
        if self.isPaused:
            self.pauseButton.SetLabel("Restart")
        else:
            self.pauseButton.SetLabel("Pause")

    def Browse(self, event):
        dialog=wx.DirDialog(None,u"Choose a directory:",os.getcwd(),style=wx.DD_DEFAULT_STYLE|wx.DD_NEW_DIR_BUTTON)
        if dialog.ShowModal()==wx.ID_OK:
            self.pathText.SetValue(dialog.GetPath())
        dialog.Destroy()

    def Validate(self, event):
        ok = True
        svr = self.serverText.GetValue()
        if svr == '':
            self.Message("please enter the server domain\n")
            ok = False
#        port = string.atoi(self.portText.GetValue(),10)
        port = self.portText.GetValue()
        if port == '':
            self.Message("please enter the port of server\n")
            ok = False
        channel = self.channelText.GetValue()
        if channel == '':
            self.Message("please enter the channel ID\n")
            ok = False
        path = self.pathText.GetValue()
        if path == '':
            self.Message("please enter the path of AVI files\n")
            ok = False
        type = self.typeChoice.GetCurrentSelection()
        if type == -1:
            self.Message("please choose a AVI type\n")
            ok = False

        if ok:
            self.Record(svr, string.atoi(port, 10), channel, path, type)

    def Record(self, svr, port, channel, path, type):
        recorder = RecorderCtrl.Recorder(svr, port, channel, path, type, self)
        recordTimer = Timer.RecordTimer(self)
        self.threads.append(recorder)
        self.threads.append(recordTimer)
        recorder.start()
        recordTimer.start()

    def Exit(self,event):
        while self.threads:
            thread = self.threads[0]
            thread.stop()
            self.threads.remove(thread)

        self.Message('exit...\n')
        time.sleep(1)
        self.Close()

    def ExitAfterRecording(self):
        self.Message('recording completed!\n')
        while self.threads:
            thread = self.threads[0]
            thread.stop()
            self.threads.remove(thread)
        if self.autoCheck.GetValue() == True:
            self.Message('exit...\n')
            self.Close()
        else:
            self.Message('please click the Exit button to exit\n')

    def Message(self, msg):
        self.info.AppendText(msg)

    def SetTimer(self, time):
        self.timeLabel.SetLabel(time)

    def AddVSize(self, size):
        self.vSize += size
        self.vSizeLabel.SetLabel(str(self.vSize)+"B")

    def AddSSize(self, size):
        self.sSize += size
        self.sSizeLabel.SetLabel(str(self.sSize)+"B")
